package cn.devzyh.oms.common.mybatis.domain;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;

import java.util.LinkedList;
import java.util.List;

/**
 * 分页查询参数
 */
public class PageQuery<T> {

	/**
	 * 当前页，起始值 1
	 */
	@Setter
	private Long current = 1L;

	/**
	 * 每页显示条数，默认 10
	 */
	@Setter
	private Long size = 10L;

	/**
	 * 升序字段，英文逗号间隔
	 */
	@Setter
	private String ascs;

	/**
	 * 降序字段，英文逗号间隔
	 */
	@Setter
	private String descs;

	/**
	 * 是否查询总行数
	 */
	@Setter
	private Boolean searchCount = true;

	/**
	 * 构造MybatisPlus分页对象
	 *
	 * @return
	 */
	public Page<T> mybatisPlusPage() {
		Page<T> page = Page.of(this.current, this.size);
		List<OrderItem> orders = new LinkedList<>();
		if (StrUtil.isNotBlank(this.descs)) {
			for (String desc : this.descs.split(",")) {
				orders.add(OrderItem.desc(desc));
			}
		}
		if (StrUtil.isNotBlank(this.ascs)) {
			for (String asc : this.ascs.split(",")) {
				orders.add(OrderItem.asc(asc));
			}
		}
		if (CollUtil.isNotEmpty(orders)) {
			page.setOrders(orders);
		}
		page.setSearchCount(this.searchCount);
		return page;
	}

}
